package com.woniuxy.yoga.admin.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.yoga.admin.model.dto.AdminAccountLoginDTO;
import com.woniuxy.yoga.admin.model.dto.adminaccount.AdminAccountAddDTO;
import com.woniuxy.yoga.admin.model.dto.adminaccount.AdminAccountLoginToken;
import com.woniuxy.yoga.admin.model.dto.adminaccount.AdminByAccountIdDTO;
import com.woniuxy.yoga.admin.model.form.AdminAccountLoginForm;
import com.woniuxy.yoga.admin.model.form.adminaccount.*;
import com.woniuxy.yoga.admin.model.param.AdminAccountLoginParam;
import com.woniuxy.yoga.admin.model.param.adminaccount.*;
import com.woniuxy.yoga.admin.permission.Permission;
import com.woniuxy.yoga.commons.core.model.PageInfo;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.commons.core.util.JWTUtil;
import com.woniuxy.yoga.commons.redis.util.RedisUtil;
import com.woniuxy.yoga.commons.web.log.YogaLog;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import com.woniuxy.yoga.admin.service.AdminAccountService;


import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 * 管理员账号表 前端控制器
 * </p>
 *
 * @author 韦用文, 刘存益, 郎腾霄
 * @since 2023年11月28日
 */
@RestController
@RequestMapping("/adminAccount")
@Api(tags = "管理员账号相关接口")
public class AdminAccountController {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private AdminAccountService adminAccountService;

    /**
     * 管理员端账号密码登录
     *
     * @param form
     * @return
     */
    @PostMapping("/loginAdmin.api")
    @ApiOperation("管理员端账号密码登录")
    @YogaLog("管理员端账号密码登录")
    public Result<AdminAccountLoginDTO> loginAdmin(@Valid @RequestBody AdminAccountLoginForm form, HttpServletResponse resp) throws Exception {
        AdminAccountLoginDTO dto = adminAccountService.loginAdmin(BeanUtil.toBean(form, AdminAccountLoginParam.class));
        Map<String, Object> map = BeanUtil.beanToMap(BeanUtil.toBean(dto, AdminAccountLoginToken.class));
        //生成jwt令牌
        String adminToken = JWTUtil.createAdminToken(map, 30);
        String refreshToken = JWTUtil.createAdminToken(map, 3 * 24 * 60);
        redisUtil.set(adminToken, refreshToken, 3 * 24 * 60 * 60L);
        resp.setHeader("authen_token", adminToken);
        resp.setHeader("Access-Control-Expose-Headers", "authen_token");
        return Result.getResult(AdminAccountLoginDTO.class).setMessage("管理员登录成功").setData(dto);
    }

    /**
     * 平台端添加管理员
     *
     * @param
     * @param
     * @return
     */
    @PostMapping("/addAdmin.api")
    @ApiOperation("平台端添加管理员")
    @YogaLog("平台端添加管理员")
    @Permission(resources = {"/admin/adminAccount/addAdmin.api"})
    public Result<AdminAccountAddDTO> addAdmin(@Valid @RequestBody AdminAccountAddForm form, @RequestHeader String authen_token) {
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        AdminAccountAddParam param = BeanUtil.toBean(form, AdminAccountAddParam.class);
        param.setAddAccountId(accountId);
        AdminAccountAddDTO dto = adminAccountService.addAdmin(param);
        return Result.getResult(AdminAccountAddDTO.class).setData(dto).setMessage("新增普通管理员成功");
    }

    /**
     * 平台端删除管理员
     *
     * @param form
     * @param authen_token
     * @return
     */
    @PostMapping("/deleteAdmin.api")
    @ApiOperation("平台端删除管理员")
    @YogaLog("平台端删除管理员")
    @Permission(resources = {"/admin/adminAccount/deleteAdmin.api"})
    public Result<String> deleteAdmin(@Valid @RequestBody AdminAccountDeleteForm form, @RequestHeader String authen_token) {
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        AdminAccountDeleteParam param = BeanUtil.toBean(form, AdminAccountDeleteParam.class);
        param.setDeleteAccountId(accountId);
        adminAccountService.deleteAdmin(param);
        return Result.getResult(String.class).setMessage("删除成功");
    }

    /**
     * 分页查询管理员
     *
     * @return
     */
    @PostMapping("/selectAdmin.api")
    @ApiOperation("分页查询管理员")
    @YogaLog("分页查询管理员")
    @Permission(resources = {"/admin/adminAccount/selectAdmin.api"})
    public Result<PageInfo> selectAdmin(@RequestBody AdminAccountPageForm form) {
        PageInfo dtos = adminAccountService.selectAdmin(BeanUtil.toBean(form, AdminAccountPageParam.class));
        return Result.getResult(PageInfo.class).setMessage("查询成功").setData(dtos);
    }

    /**
     * 修改平台管理员密码
     *
     * @param form
     * @param authen_token
     * @return
     */
    @PostMapping("/updateAdminPass.api")
    @ApiOperation("修改平台管理员密码")
    @YogaLog("修改管理员密码")
    public Result<String> updateAdminPass(@Valid @RequestBody AdminPassUpdateForm form, @RequestHeader String authen_token) {
        String refreshToken = redisUtil.get(authen_token);
        Integer accountId = JWTUtil.getAdminId(refreshToken);
        AdminPassUpdateParam param = BeanUtil.toBean(form, AdminPassUpdateParam.class);
        param.setAccountId(accountId);
        adminAccountService.updateAdminPass(param);
        redisUtil.delete(authen_token);
        return Result.getResult(String.class).setMessage("修改成功");
    }

    /**
     * 禁止管理员
     *
     * @param form
     * @return
     */
    @PostMapping("/disableAdminAccount.api")
    @ApiOperation("禁用管理员")
    @YogaLog("禁用管理员")
    @Permission(resources = {"/admin/adminAccount/disableAdminAccount.api"})
    public Result<String> disableAdminAccount(@RequestBody AdminAccountDisableForm form) {
        adminAccountService.disableAdminAccount(BeanUtil.toBean(form, AdminAccountDisableParam.class));
        return Result.getResult(String.class).setMessage("禁用成功");
    }

    /**
     * 启用管理员
     *
     * @param form
     * @return
     */
    @PostMapping("/updAdminAccount.api")
    @ApiOperation("启用管理员")
    @YogaLog("启用管理员")
    @Permission(resources = {"/admin/adminAccount/updAdminAccount.api"})
    public Result<String> updAdminAccount(@Valid @RequestBody AdminAccountUpdForm form) {
        adminAccountService.updAdminAccount(BeanUtil.toBean(form, AdminAccountUpdParam.class));
        return Result.getResult(String.class).setMessage("启用成功");
    }

    /**
     * 根据管理员账号ID查询正常账号
     *
     * @param accountId
     * @return
     * @throws Exception
     */
    @GetMapping("/selAdminByAccountId/{accountId}")
    @ApiOperation("根据账号ID查询信息")
    @YogaLog("根据账号ID查询信息")
    public Result<AdminByAccountIdDTO> selAdminByAccountId(@PathVariable("accountId") Integer accountId) throws Exception {
        AdminByAccountIdDTO dto = adminAccountService.selAdminByAccountId(accountId);
        return Result.getResult(AdminByAccountIdDTO.class).setMessage("查询成功").setData(dto);
    }

    /**
     * 测试时间参数传递
     *
     * @param form
     * @return
     */
    @PostMapping("/test.api")
    public Result<String> test(@RequestBody TestForm form) {
        System.out.println(form);
        return Result.getResult(String.class).setMessage("测试成功");
    }

    /**
     * 验证密码
     *
     * @param form         参数
     * @param authen_token 令牌
     * @return string
     * @throws Exception
     */
    @PostMapping("/checkOldPwd.api")
    @ApiOperation("查询密码是否正确")
    @YogaLog("查询密码是否正确")
    public Result<String> checkOldPwd(@Valid @RequestBody CheckAdminPassForm form, @RequestHeader("authen_token") String authen_token) throws Exception {
        Integer accountId = JWTUtil.getAdminId(authen_token);
        CheckAdminPassParam param = BeanUtil.toBean(form, CheckAdminPassParam.class);
        param.setAccountId(accountId);
        adminAccountService.checkOldPwd(param);
        return Result.getResult(String.class).setMessage("密码验证成功");
    }

    /**
     * 管理员退出登录
     *
     * @param authen_token
     * @return
     */
    @PostMapping("/logOut.api")
    @ApiOperation("管理员退出登录")
    @YogaLog("管理员退出登录")
    public Result<String> logOut(@RequestHeader String authen_token) {
        redisUtil.delete(authen_token);
        return Result.getResult(String.class).setMessage("退出登录成功");
    }


}

